package com.yzl.meeting.business.controller;

import com.yzl.meeting.business.entity.Permission;
import com.yzl.meeting.business.entity.Role;
import com.yzl.meeting.business.rule.DataResult;
import com.yzl.meeting.business.service.Impl.RolePermissionServiceImpl;
import com.yzl.meeting.business.service.RolePermissionService;
import com.yzl.meeting.business.vo.RolePerVo;
import com.yzl.meeting.framework.annotation.Controller;
import com.yzl.meeting.framework.annotation.RequestBody;
import com.yzl.meeting.framework.annotation.RequestMapping;

import java.sql.SQLException;
import java.util.List;

import static com.yzl.meeting.business.rule.ResponseCode.*;

/**
 * @Title: RolePermissionController
 * @Author: 姚志立
 * @Description:
 * @Date: 2020/10/12 16:37
 * @Version: 1.0
 */

@Controller("/RolePermission")
public class RolePermissionController {
    RolePermissionService rolePermissionService = new RolePermissionServiceImpl();

    @RequestMapping("/addPer")
    public DataResult addPer(@RequestBody Permission permission) {
        DataResult dataResult = null;
        try {
            int i = rolePermissionService.addPer(permission);
            dataResult = new DataResult();
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/addRole")
    public DataResult addRole(@RequestBody RolePerVo rolePerVo) {
        DataResult dataResult = null;
        try {
            rolePermissionService.addRole(rolePerVo);
            dataResult = new DataResult();
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/listRoles")
    public DataResult<Role> listRoles() {
        DataResult dataResult = null;
        try {
            List<Role> roles = rolePermissionService.listRoles();
            dataResult = new DataResult<>(roles);
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/listPer")
    public DataResult<Permission> listPer() {
        DataResult dataResult = null;
        try {
            List<Permission> roles = rolePermissionService.listPer();
            dataResult = new DataResult<>(roles);
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/deletePer")
    public DataResult deletePer(Permission permission){
        DataResult dataResult=null;
        try {
            rolePermissionService.deletePer(permission.getPerId());
            dataResult=new DataResult();
        }catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(DELETEEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }


    @RequestMapping("/deleteRole")
    public DataResult deleteRole(Role role){
        DataResult dataResult=null;
        try {
            rolePermissionService.deleteRole(role.getRoleId());
            dataResult=new DataResult();
        }catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }
}

